package com.jason.orc;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.baidu.aip.ocr.AipOcr;
import com.jason.orc.enums.BaiduOrcHandleTypeEnum;
import com.jason.orc.vo.BaiduAccessTokenVo;
import com.jason.orc.vo.BaiduOrcVo;

import java.util.HashMap;

import static com.jason.orc.enums.BaiduOrcHandleTypeEnum.BASIC_ORC;
/**
 * 百度 ORC识别 客户端
 * @author JasonChoi
 */
public class BaiduOrcClient extends AipOcr {
    /**
     * 鉴权认证
     */
    private static final String ACCESS_TOKEN_URL = "https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id={}&client_secret={}";
    private static String accessToken = "";

    public BaiduOrcClient(String appId, String apiKey, String secretKey) {
        super(appId, apiKey, secretKey);
    }

    public BaiduOrcVo orcByImagePath(String imagePath) {
        return orcByImagePath(imagePath, BASIC_ORC);
    }

    public BaiduOrcVo orcByImagePath(String imagePath, BaiduOrcHandleTypeEnum typeEnum) {
        String respStr = "";
        switch (typeEnum) {
            case BASIC_ORC:
                respStr = basicGeneral(imagePath, new HashMap<>(0)).toString();
                break;
            case INCLUDE_LOCATION_BASIC_ORC:
                respStr = general(imagePath, new HashMap<>(0)).toString();
                break;
            case ACCURATE_BASIC:
                respStr = basicAccurateGeneral(imagePath, new HashMap<>(0)).toString();
                break;
            case INCLUDE_LOCATION_ACCURATE_ORC:
                respStr = accurateGeneral(imagePath, new HashMap<>(0)).toString();
                break;
            case ENHANCED_ORC:
                respStr = enhancedGeneral(imagePath, new HashMap<>(0)).toString();
                break;
            default:
        }
        return JSONObject.parseObject(respStr,BaiduOrcVo.class);
    }

    public BaiduOrcVo orcByImageUrl(String imageUrl){
        return orcByImageUrl(imageUrl, BASIC_ORC);
    }

    public BaiduOrcVo orcByImageUrl(String imageUrl, BaiduOrcHandleTypeEnum typeEnum) {
        String respStr = "";
        switch (typeEnum) {
            case BASIC_ORC:
                respStr = basicGeneralUrl(imageUrl, new HashMap<>(0)).toString();
                break;
            case INCLUDE_LOCATION_BASIC_ORC:
                respStr = generalUrl(imageUrl, new HashMap<>(0)).toString();
                break;
            case ENHANCED_ORC:
                respStr = enhancedGeneralUrl(imageUrl, new HashMap<>(0)).toString();
                break;
            default:
        }
        return JSONObject.parseObject(respStr,BaiduOrcVo.class);
    }
}
